 /*****************************************************************************
 *  This file is part of the OpenDomo project.
 *  Copyright(C) 2011 OpenDomo Services SL
 *  
 *  Daniel Lerch Hostalot <dlerch@opendomo.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/



// ----------------------------------------------------------------------------
// author: Daniel Lerch <dlerch@gmail.com>, http://www.opendomo.com
// date: APRIL 2009
//
// CHANGES:
//
// ----------------------------------------------------------------------------


#ifndef __STACK_H__
#define __STACK_H__



#include <stdio.h>
#include <stdlib.h>


#define STACK_CREATE(varname, type) \
   typedef struct __##varname \
   { \
      type value; \
      struct __##varname *next; \
   } \
   _##varname; \
   _##varname *varname = NULL;

#define STACK_PTR STACK_CREATE

#define STACK_PUSH(varname,varvalue) \
   { \
      _##varname * varname##_new_node = malloc(sizeof(_##varname)); \
      varname##_new_node->value = varvalue; \
      varname##_new_node->next = varname; \
      varname = varname##_new_node; \
   }


#define STACK_GET(varname) varname->value

#define STACK_POP(varname) \
   { \
      _##varname *head = varname; \
      varname = varname->next; \
      free(head); \
   }


#define STACK_DESTROY(varname) \
   { \
      _##varname *prev = NULL; \
      _##varname *it = NULL; \
      for(it=varname; it!=NULL; it=it->next) \
      { \
         if(prev) \
            free(prev); \
         prev = it; \
      } \
    if(prev) \
         free(prev); \
   }


#endif

